package gui;

import javax.swing.*;
import java.awt.*;
import javax.swing.event.*;

import main.TwoDGrid;

/**
 *
 * @author Arwa Tyebkhan
 */
public class HeatPad extends JPanel implements ChangeListener {

  private static final int MAX_TEMP = 100;
  private static final int MIN_TEMP = 0;
  private static final int INITIAL_TEMP = 0;
  
  private int topTemperature = 0;
  private int rightTemperature = 0;
  private int leftTemperature = 0;
  private int bottomTemperature = 0;
  
  private HeatGrid heatGrid;
  private int defaultMax = 100;

  /** Creates a new instance of HeatPad */
  public HeatPad() {
    this.setSize(HeatGrid.GRID_COORD_SIZE, HeatGrid.GRID_COORD_SIZE);
    this.setLayout(new BorderLayout());
    
    addHeatGrid();
    addHeatControls();    
  }

  private void addHeatControls() {
    /* Adding the 4 temperature controls */
    JSlider topTemperature = new JSlider(JSlider.HORIZONTAL, MIN_TEMP, MAX_TEMP, INITIAL_TEMP);
    addHeatControl(topTemperature, BorderLayout.NORTH);

    JSlider rightTemperature = new JSlider(JSlider.VERTICAL, MIN_TEMP, MAX_TEMP, INITIAL_TEMP);
    addHeatControl(rightTemperature, BorderLayout.EAST);

    JSlider bottomTemperature = new JSlider(JSlider.HORIZONTAL, MIN_TEMP, MAX_TEMP, INITIAL_TEMP);
    addHeatControl(bottomTemperature, BorderLayout.SOUTH);

    JSlider leftTemperature = new JSlider(JSlider.VERTICAL, MIN_TEMP, MAX_TEMP, INITIAL_TEMP);
    addHeatControl(leftTemperature, BorderLayout.WEST);
  }
  
  private void addHeatControl(JSlider slider, String layout) {
    this.add(slider, layout);
    slider.setName(layout);
    setTemperatureOptions(layout, INITIAL_TEMP);
    
    slider.setMajorTickSpacing(10);
    slider.setMinorTickSpacing(2);
    slider.setPaintTicks(true);
    slider.setPaintLabels(true);
    slider.addChangeListener(this);
  }
  
  private void setTemperatureOptions(String name, int temperature) {
    if(name.equals(BorderLayout.NORTH)) {
      topTemperature = temperature;
      System.out.println("Top: " + temperature);
    }
    else if(name.equals(BorderLayout.SOUTH)) {
      bottomTemperature = temperature;
      System.out.println("Bottom: " + temperature);
    }
    else if(name.equals(BorderLayout.EAST)) {
      rightTemperature = temperature;
      System.out.println("Right: " + temperature);
    }
    else if(name.equals(BorderLayout.WEST)) {
      leftTemperature = temperature;
      System.out.println("Left: " + temperature);
    }
  }

  private void addHeatGrid() {
      heatGrid = new HeatGrid(5,defaultMax);
      add(heatGrid, BorderLayout.CENTER);
  }
  
  public void setHeatGridLattice(int dimensions, int maxCount) {
    heatGrid.setSize(dimensions);
    heatGrid.setMaxCount(maxCount);
  }

  
  public void run(TwoDGrid grid, boolean animate, JTextField counter) {
    heatGrid.run(grid, animate, counter);
  }
  
  public int getTopTemperature() {
    return topTemperature;
  }

  public int getBottomTemperature() {
    return bottomTemperature;
  }

  public int getLeftTemperature() {
    return leftTemperature;
  }

  public int getRightTemperature() {
    return rightTemperature;
  }
  
  protected void addHeatPad(int size) {
      heatGrid.setSize(size);
      heatGrid.setMaxCount(defaultMax);
      heatGrid.repaint();
      add(heatGrid, BorderLayout.CENTER);
  }          
  
  public void stateChanged(ChangeEvent e) {
    JSlider source = (JSlider) e.getSource();
    if (!source.getValueIsAdjusting()) {
      int temperature = source.getValue();  
      setTemperatureOptions(source.getName(), temperature);      
    }
  }
}